"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _useMergedState3 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));

var _react = _interopRequireWildcard(require("react"));

var _Select = require("../Select");

var _excluded = ["radioType", "renderFormItem", "mode", "light", "label", "render"],
    _excluded2 = ["onSearch", "onClear", "onChange", "onBlur", "showSearch", "autoClearSearchValue", "treeData", "searchValue"];

/**
 * Tree select
 * A function that returns a React component.
 * @param ref
 */
var FieldTreeSelect = function FieldTreeSelect(_ref, ref) {
  var radioType = _ref.radioType,
      renderFormItem = _ref.renderFormItem,
      mode = _ref.mode,
      light = _ref.light,
      label = _ref.label,
      render = _ref.render,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var layoutClassName = getPrefixCls('pro-field-tree-select');
  var treeSelectRef = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var _ref2 = rest.fieldProps || {},
      onSearch = _ref2.onSearch,
      _onClear = _ref2.onClear,
      propsOnChange = _ref2.onChange,
      _onBlur = _ref2.onBlur,
      showSearch = _ref2.showSearch,
      autoClearSearchValue = _ref2.autoClearSearchValue,
      treeData = _ref2.treeData,
      propsSearchValue = _ref2.searchValue,
      fieldProps = (0, _objectWithoutProperties2.default)(_ref2, _excluded2);

  var size = (0, _react.useContext)(_antdV.ConfigProvider.SizeContext);

  var _useFieldFetchData = (0, _Select.useFieldFetchData)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, rest), {}, {
    defaultKeyWords: propsSearchValue
  })),
      _useFieldFetchData2 = (0, _slicedToArray2.default)(_useFieldFetchData, 3),
      loading = _useFieldFetchData2[0],
      options = _useFieldFetchData2[1],
      _fetchData = _useFieldFetchData2[2];

  var _useMergedState = (0, _useMergedState3.default)('', {
    onChange: onSearch,
    value: propsSearchValue
  }),
      _useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 2),
      searchValue = _useMergedState2[0],
      setSearchValue = _useMergedState2[1];

  (0, _react.useImperativeHandle)(ref, function () {
    return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, treeSelectRef.current || {}), {}, {
      fetchData: function fetchData() {
        return _fetchData();
      }
    });
  });
  var optionsValueEnum = (0, _react.useMemo)(function () {
    if (mode !== 'read') return;
    /**
     * Support TreeSelect fieldNames
     * @see https://ant.design/components/tree-select-cn
     */

    var _ref3 = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.fieldNames) || {},
        _ref3$value = _ref3.value,
        valuePropsName = _ref3$value === void 0 ? 'value' : _ref3$value,
        _ref3$label = _ref3.label,
        labelPropsName = _ref3$label === void 0 ? 'label' : _ref3$label,
        _ref3$children = _ref3.children,
        childrenPropsName = _ref3$children === void 0 ? 'children' : _ref3$children;

    var valuesMap = new Map();

    var traverseOptions = function traverseOptions(_options) {
      if (!(_options === null || _options === void 0 ? void 0 : _options.length)) {
        return valuesMap;
      }

      var length = _options.length;
      var i = 0;

      while (i < length) {
        var cur = _options[i++];
        valuesMap.set(cur[valuePropsName], cur[labelPropsName]);
        traverseOptions(cur[childrenPropsName]);
      }

      return valuesMap;
    };

    return traverseOptions(options);
  }, [fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.fieldNames, mode, options]);

  var onChange = function onChange(value, optionList, extra) {
    // 将搜索框置空 和 antd 行为保持一致
    if (showSearch && autoClearSearchValue) {
      _fetchData('');

      setSearchValue('');
    }

    propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange(value, optionList, extra);
  };

  if (mode === 'read') {
    var dom = (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: (0, _Select.proFieldParsingText)(rest.text, (0, _Select.ObjToMap)(rest.valueEnum || optionsValueEnum))
    });

    if (render) {
      return render(rest.text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), dom) || null;
    }

    return dom;
  }

  if (mode === 'edit') {
    var _fieldProps$value;

    var valuesLength = Array.isArray(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) ? fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$value = fieldProps.value) === null || _fieldProps$value === void 0 ? void 0 : _fieldProps$value.length : 0;

    var _dom = (0, _jsxRuntime.jsx)(_antdV.Spin, {
      spinning: loading,
      children: (0, _jsxRuntime.jsx)(_antdV.TreeSelect, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        open: open,
        onDropdownVisibleChange: setOpen,
        ref: treeSelectRef,
        dropdownMatchSelectWidth: !light,
        tagRender: light ? function (item) {
          var _fieldProps$value2;

          if (valuesLength < 2) return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
            children: item.label
          });
          /**
           * 性能不好，等我给 antd 提个issue
           */

          var itemIndex = fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$value2 = fieldProps.value) === null || _fieldProps$value2 === void 0 ? void 0 : _fieldProps$value2.findIndex(function (v) {
            return v === item.value || v.value === item.value;
          });
          return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
            children: [item.label, " ", itemIndex < valuesLength - 1 ? ',' : '']
          });
        } : undefined
      }, fieldProps), {}, {
        bordered: !light,
        treeData: options,
        showSearch: showSearch,
        style: (0, _objectSpread2.default)({
          minWidth: 60
        }, fieldProps.style),
        searchValue: searchValue,
        autoClearSearchValue: autoClearSearchValue,
        onClear: function onClear() {
          _onClear === null || _onClear === void 0 ? void 0 : _onClear();

          _fetchData('');

          if (showSearch) {
            setSearchValue('');
          }
        },
        onChange: onChange,
        onSearch: function onSearch(value) {
          _fetchData(value);

          setSearchValue(value);
        },
        onBlur: function onBlur(event) {
          setSearchValue('');

          _fetchData('');

          _onBlur === null || _onBlur === void 0 ? void 0 : _onBlur(event);
        },
        className: (0, _classnames.default)(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.className, layoutClassName)
      }))
    });

    if (renderFormItem) {
      _dom = renderFormItem(rest.text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), _dom) || null;
    }

    if (light) {
      var disabled = fieldProps.disabled,
          allowClear = fieldProps.allowClear,
          placeholder = fieldProps.placeholder;
      return (0, _jsxRuntime.jsx)(_proUtils.FieldLabel, {
        label: label,
        disabled: disabled,
        placeholder: placeholder,
        size: size,
        onLabelClick: function onLabelClick() {
          return setOpen(!open);
        },
        allowClear: allowClear,
        bordered: rest.bordered,
        value: _dom,
        onClear: function onClear() {
          return propsOnChange === null || propsOnChange === void 0 ? void 0 : propsOnChange(undefined, [], {});
        }
      });
    }

    return _dom;
  }

  return null;
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldTreeSelect);

exports.default = _default;